package service

import (
	"anban/models"
	"anban/utils"
	"github.com/astaxie/beego/orm"
	"net/url"
)

// 获取提现记录信息
func GetDeductionRecordInfo(p map[string]interface{}) *models.DeductionRecord {
	o := orm.NewOrm()
	deductionRecord := &models.DeductionRecord{}
	qs := o.QueryTable("DeductionRecord")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["id"]; ok {
		qs = qs.Filter("id", p["id"])
	}
	if _, ok := p["sn"]; ok {
		qs = qs.Filter("sn", p["sn"])
	}
	qs.One(deductionRecord)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		if deductionRecord.Id > 0 {
			deductionRecord.StatusShow = models.DeductionRecordStatus[deductionRecord.Status]
			deductionRecord.CreatedShow = deductionRecord.Created.Format("2006-01-02 15:04:05")
			deductionRecord.CreatedShowDate = deductionRecord.Created.Format("2006.1.2")
		}
	}
	return deductionRecord
}

// 添加提现记录信息
func AddDeductionRecord(input url.Values) (int64, error) {
	o := orm.NewOrm()
	deductionRecord := &models.DeductionRecord{}

	sn := utils.GetSn()
	p := map[string]interface{}{}
	p["sn"] = sn
	deductionRecordInfo := GetDeductionRecordInfo(p)
	for deductionRecordInfo.Id > 0 {
		sn = utils.GetSn()
		p = map[string]interface{}{}
		p["sn"] = sn
		deductionRecordInfo = GetDeductionRecordInfo(p)
	}
	deductionRecord.Sn = sn

	user := &models.User{}
	if _, ok := input["user_id"]; ok {
		user.Id = utils.Atoi64(input["user_id"][0])
		o.Read(user)
	}
	deductionRecord.User = user

	fromUser := &models.User{}
	if _, ok := input["from_user_id"]; ok {
		fromUser.Id = utils.Atoi64(input["from_user_id"][0])
		o.Read(fromUser)
	}
	deductionRecord.FromUser = fromUser

	student := &models.Student{}
	if _, ok := input["student_id"]; ok {
		student.Id = utils.Atoi64(input["student_id"][0])
		o.Read(student)
	}
	deductionRecord.Student = student

	if _, ok := input["amount"]; ok {
		deductionRecord.Amount = utils.Atof64(input["amount"][0])
	}
	return o.Insert(deductionRecord)
}

// 修改提现记录信息
func EditDeductionRecord(input url.Values) (int64, error) {
	o := orm.NewOrm()
	p := orm.Params{}
	for k, v := range input {
		if k == "id" || k == "_xsrf" {
			continue
		}
		p[k] = v[0]
	}
	return o.QueryTable("DeductionRecord").Filter("id", input["id"][0]).Update(p)
}

// 获取提现记录列表
func GetDeductionRecordList(p map[string]interface{}) (int64, []*models.DeductionRecord) {
	var deductionRecordList []*models.DeductionRecord
	o := orm.NewOrm()
	qs := o.QueryTable("DeductionRecord")
	if _, ok := p["relation"]; ok && p["relation"].(bool) {
		qs = qs.RelatedSel()
	}
	if _, ok := p["userId"]; ok {
		qs = qs.Filter("user_id", p["userId"])
	}
	if _, ok := p["realName"]; ok {
		qs = qs.Filter("User__RealName", p["realName"])
	}
	if _, ok := p["phone"]; ok {
		qs = qs.Filter("User__Phone", p["phone"])
	}
	if _, ok := p["fromRealName"]; ok {
		qs = qs.Filter("FromUser__RealName", p["fromRealName"])
	}
	if _, ok := p["fromPhone"]; ok {
		qs = qs.Filter("FromUser__Phone", p["fromPhone"])
	}
	if _, ok := p["status"]; ok {
		qs = qs.Filter("status", p["status"])
	}
	if _, ok := p["studentRealName"]; ok {
		qs = qs.Filter("Student__RealName", p["studentRealName"])
	}
	totalCount, _ := qs.Count()
	curPage, ok := p["curPage"]
	perCount, _ := p["perCount"]
	if ok {
		start := perCount.(int) * (curPage.(int) - 1)
		qs = qs.Limit(perCount, start)
	}
	qs.OrderBy("-id").All(&deductionRecordList)
	if _, ok := p["convert"]; ok && p["convert"].(bool) {
		for _, deductionRecord := range deductionRecordList {
			deductionRecord.StatusShow = models.DeductionRecordStatus[deductionRecord.Status]
			deductionRecord.CreatedShow = deductionRecord.Created.Format("2006-01-02 15:04:05")
			deductionRecord.CreatedShowDate = deductionRecord.Created.Format("2006.1.2")
		}
	}
	if _, ok := p["api"]; ok && p["api"].(bool) {
		for _, deductionRecord := range deductionRecordList {
			deductionRecord.RelationName = deductionRecord.FromUser.StudentRelation.Name
			deductionRecord.StudentName = deductionRecord.Student.RealName
			deductionRecord.StatusShow = models.DeductionRecordStatus[deductionRecord.Status]
			deductionRecord.CreatedShowDate = deductionRecord.Created.Format("2006.1.2")
		}
	}
	return totalCount, deductionRecordList
}